#include "Stack.h"

void StackInit(Stack* st)
{
	st->arr = NULL;
	st->capacity = 0;
	st->top = 0;
}
void StackDestroy(Stack* st)
{
	free(st->arr);
	st->arr = NULL;
	st->top = 0;
	st->capacity = 0;
}

void StackPush(Stack* st, StackDataType x)
{
	assert(st);
	if (st->top == st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : 2 * st->capacity;
		StackDataType* tmp = (StackDataType*)realloc(st->arr, newcapacity * sizeof(StackDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		st->arr = tmp;
		st->capacity = newcapacity;
	}
	st->arr[st->top++] = x;
}

void StackPop(Stack* st)
{
	assert(st);
	assert(st->top > 0);
	st->top--;
}
StackDataType StackTop(Stack* st)
{
	return st->arr[st->top - 1];
}

int StackSize(Stack* st)
{
	return st->top;
}

bool StackEmpty(Stack* st)
{
	return st->top == 0;
}

void StackPrint(Stack* st)
{
	while (!StackEmpty(st))
	{
		printf("%d ", StackTop(st));
		StackPop(st);
	}
	printf("\n");
}

